package com.legrand.user.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.legrand.core.entity.User;
import com.legrand.core.exception.CustomizeException;
import com.legrand.core.util.BaseResponse;
import com.legrand.core.util.EncryptionUtil;
import com.legrand.user.service.UserService;
import com.legrand.user.dto.UserNamePasswordDTO;
import com.legrand.user.mapper.UserMapper;
import com.legrand.user.util.TokenUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
/**
*@author xiah
*2018-07-11 02:23:06
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    Logger logger=Logger.getLogger(UserServiceImpl.class);
    Map<String, Boolean> map = new HashMap<>(16);
    @Autowired
    private TokenUtil tokenUtil;

    @Override
    public User getTeacher() {
        /*   使用mybatis-plus分页
              EntityWrapper wrapper = new EntityWrapper();
              wrapper.where("id>{0}", 0);
              从第一条记录开始   不包括第一条   最多返回三条记录
              RowBounds rowBounds = new RowBounds(1, 3);
              List list = teacherMapper.selectPage(rowBounds, wrapper);

               使用自定义分页 当前页 每页多少条
              Page page = new Page(1, 3);
              List list2 = teacherMapper.getList(page);
              Teacher teacher = teacherMapper.selectById(1L);*/
        return new User();
    }

    /**
     * @author xiah
     * 2018-07-10 10:11:38
     * 根据用户名查找用户
     */
    @Override
    public BaseResponse getByUserName(String userName) {
        EntityWrapper ew=new EntityWrapper(new User());
        ew.where("user_name = {0}",userName);
        if (this.selectOne(ew) != null) {
            new BaseResponse(map.put("exit", true));
        }
        return new BaseResponse(map.put("exit", false));
    }

    /**
     * 用户名密码注册，注册成功返回token给客户端
     */
    @Override
    public BaseResponse registerByUserNameAndPassword(UserNamePasswordDTO userNamePasswordDTO) {
        User user = new User();
        EntityWrapper ew=new EntityWrapper(user);
        ew.where("user_name = {0}",userNamePasswordDTO.getUserName());
        if (this.selectOne(ew) != null) {
            throw new CustomizeException("该用户名已存在");
        }
        if (!userNamePasswordDTO.getRePassword().equals(userNamePasswordDTO.getPassword())) {
            throw new CustomizeException("两次输入的密码不一致");
        }
        user.setUserName(userNamePasswordDTO.getUserName());
        user.setPassword(EncryptionUtil.md5(userNamePasswordDTO.getPassword()));
        /**用户添加成功，返回对应的token给客户端*/
        if (insert(user)) {
            return new BaseResponse(tokenUtil.getToken(user));
        }
        throw new CustomizeException("服务器异常，用户添加失败");
    }

    @Override
    public BaseResponse loginByUserNameAndPassword(UserNamePasswordDTO userNamePasswordDTO) {
        User user = new User();
        EntityWrapper ew=new EntityWrapper(user);
        ew.where("user_name = {0}",userNamePasswordDTO.getUserName());
        user=this.selectOne(ew);
        if(user!=null&&user.getPassword().equals(EncryptionUtil.md5(userNamePasswordDTO.getPassword()))){
            return new BaseResponse(tokenUtil.getToken(user));
        }
        throw new CustomizeException("用户名或密码错误");
    }
}
